package com.example.happiness.service.admin.Impl;

import com.example.happiness.mapper.ServeMapper;
import com.example.happiness.pojo.Serve;
import com.example.happiness.pojo.adminvo.AdminServeVO;
import com.example.happiness.result.Result;
import com.example.happiness.service.admin.AdminServeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminServeServiceImpl implements AdminServeService {

    @Autowired
    ServeMapper serveMapper;

    public List<AdminServeVO> selectServe() {
        return serveMapper.selectServe();
    }

    public void addServe(Serve serve) {
        serve.setCreatetime(LocalDateTime.now());
        serveMapper.insert(serve);
    }

    public Result deleteServe(Integer sid) {
        Serve serve = serveMapper.selectByPrimaryKey(sid);
        if(serve.getState() == 1){
            return Result.error("该服务已启用，无法删除");
        }
        serveMapper.deleteByPrimaryKey(sid);
        return Result.success();
    }

    public void updateServe(Serve serve) {
        try {
            serveMapper.updateByPrimaryKeySelective(serve);
        }catch (Exception e){
            serve.setCid(null);
            serveMapper.updateByPrimaryKeySelective(serve);
        }
    }
}
